# -*- coding: UTF-8 -*-
"""
@Time : 2022/12/5
@Author : Gloria
"""
from django.contrib import admin
from django.utils.safestring import mark_safe
from app01.models import Advert


# 广告
class AdvertAdmin(admin.ModelAdmin):
    def get_href(self: Advert):
        return mark_safe(f"""<a href="{self.href}" target="_blank">跳转连接</a""")

    get_href.short_description = '跳转链接'

    def get_img_list(self: Advert):
        # 解析分号;；
        # 解析换行符号\n
        html_s: str = self.img_list
        html_new = html_s.replace('；', ';').replace('\n', ';')
        html_list = html_new.split(';')

        html_str = ''
        for i in html_list:
            html_str += f'<img src="{i}" style="height: 60px;border-radius: 5px;margin-right: 5px">'
        return mark_safe(html_str)

    get_img_list.short_description = '广告图组'

    def get_img(self: Advert):
        if self.img:
            return mark_safe(f'<img src="{self.img.url}" style="height: 60px;border-radius: 5px;margin-right: 50px">')

    get_img.short_description = '用户上传'

    list_display = ['title', get_img, 'is_show', 'author', get_img_list, get_href]
